package com.tuyenmonkey.mkloader.type;

import com.tuyenmonkey.mkloader.model.Circle;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Canvas;




public class ClassicSpinner extends LoaderView {
  private Circle[] circles;
  private int circlesSize;

  public ClassicSpinner() {
    circlesSize = 8;
  }

  @Override public void initializeObjects() {
    final float size = Math.min(width, height);
    final float circleRadius = size / 10.0f;
    circles = new Circle[circlesSize];

    for (int i = 0; i < circlesSize; i++) {
      circles[i] = new Circle();
      circles[i].setCenter(center.getPointX(), circleRadius);
      circles[i].setColor(color);
      circles[i].setAlpha(126);
      circles[i].setRadius(circleRadius);
    }
  }


  @Override public void setUpAnimation() {
    for (int i = 0; i < circlesSize; i++) {
      final int index = i;
      AnimatorValue fadeAnimator =new AnimatorValue();
      fadeAnimator.setLoopedCount(AnimatorValue.INFINITE);
      fadeAnimator.setDuration(1000);
      fadeAnimator.setDelay(index * 120);
      fadeAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
        @Override
        public void onUpdate(AnimatorValue animatorValue, float v) {
          circles[index].setAlpha(v);
          if (invalidateListener != null) {
            invalidateListener.reDraw();
          }
        }
      });
      fadeAnimator.start();
    }
  }

  @Override public void draw(Canvas canvas) {
    for (int i = 0; i < circlesSize; i++) {
      canvas.save();
      canvas.rotate(45 * i, center.getPointX(), center.getPointY());
      circles[i].draw(canvas);
      canvas.restore();
    }
  }
}
